package com.mall.search.web.exceptionHandler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置全局异常处理器，全局异常处理器只有一个生效，配置多个也是只有一个生效，用它来统一处理抛出的所有异常
 *注意将其加入IOC容器中
 * @author Administrator
 */
@Component("GlobalExceptionHandler")
@Slf4j
public class GlobalExceptionHandler implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        //打印控制台
        ex.printStackTrace();

        //写日志

        //发邮件

        //发短信

        //展示错误页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/error/exception");
        return modelAndView;
    }
}
